package orxatas.travelme.sync;

import orxatas.travelme.activity.AsyncActivity;
import orxatas.travelme.manager.DataManager;
import orxatas.travelme.manager.EntryManager;
import orxatas.travelme.manager.FriendManager;
import orxatas.travelme.manager.GroupManager;

public class SyncData extends SyncOptions {
	
	/**
	 * True si hay una petición de actualización en curso. En caso contrario false.
	 * */
	static private boolean synchronizing = false;
	
	/**
	 * Contexto de la aplicación. Actividad.
	 * */
	private AsyncActivity asyncActivity;
	
	public enum SyncDataType {
		GROUPS, FRIENDS, ENTRIES
	}
	
	public SyncData(DataManager dataManager, AsyncActivity asyncActivity){
		this.asyncActivity = asyncActivity;
	}
	
	/**
	 * Synchronize a data.
	 * 
	 * @param type The data type to sync.
	 * */
	public void syncThis(SyncDataType type){
		synchronizing = true;
		
		switch(type){
		case FRIENDS:
			new FriendManager(asyncActivity).synchronize(SyncDataType.GROUPS);
			break;
		case GROUPS:
			new GroupManager(asyncActivity).synchronize(SyncDataType.ENTRIES);
			break;
		case ENTRIES:
			new EntryManager(asyncActivity).synchronize(null);
			break;
		default:
			new FriendManager(asyncActivity).synchronize(SyncDataType.FRIENDS);
		}
	}
	
	public void syncEnd(){
		synchronizing = true;
	}
	
	/**
	 * Tries to sync all
	 * */
	public void syncAll(){
		syncThis(SyncDataType.FRIENDS);
	}
	
	public boolean isSync(){
		return synchronizing;
	}
}
